﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;


public partial class Main_Company_NewCompany : System.Web.UI.Page
{
    constr123 dut = new constr123();
    protected void Page_Load(object sender, EventArgs e)
    {
       //string name = Request.QueryString["compname"].ToString();
       //lblCompanyName.Text = name.ToString();
       
        //if (Session["userid"] == null)
        //{
        //    //ScriptManager.RegisterStartupScript(Page, this.GetType(), "redirect", "window.open('../../login.aspx','_parent');", true);
        //    Response.Redirect("~/SessionExpire.aspx");
        //}
        if (CHKVat.Checked == false)
        {
            TxtBoxVat.Enabled = false;
            TxtVatDate.Enabled = false;
            ImgBtnVatDate.Enabled = false;
        }
        else
        {
            TxtBoxVat.Enabled = true;
            TxtVatDate.Enabled = true;
            ImgBtnVatDate.Enabled = true;
        }
        if (CHKInterstate.Checked == false)
        {
            TextBoxinnerState.Enabled = false;
            TextBoxInterStateDate.Enabled = false;
            ImgBtnInterState.Enabled = false;
        }
        else
        {
            TextBoxinnerState.Enabled = true;
            TextBoxInterStateDate.Enabled = true;
            ImgBtnInterState.Enabled = true;
        }

        if (CHKServiceTax.Checked == false)
        {
            TextBoxServiceTax.Enabled = false;
            TxtDateServiceTax.Enabled = false;
            ImgBtnServiceTax .Enabled = false;
        }
        else
        {
            TextBoxServiceTax.Enabled = true;
            TxtDateServiceTax.Enabled = true;
            ImgBtnServiceTax.Enabled = true;
        }

        if (CHKCentralExcise.Checked == false)
        {
            TextBoxCentralExcise.Enabled = false;
            TxtDateCentralExcise.Enabled = false;
            ImgBtnCentralExcise.Enabled = false;
        }
        else
        {
            TextBoxCentralExcise.Enabled = true;
            TxtDateCentralExcise.Enabled = true;
            ImgBtnCentralExcise.Enabled = true;
        }

        if (CHKLuxuryTax.Checked == false)
        {
            TextBoxLuxuryTax.Enabled = false;
            DateLuxuryTax.Enabled = false;
            ImgBtnLuxuryTax.Enabled = false;
        }
        else
        {
            TextBoxLuxuryTax.Enabled = true;
            DateLuxuryTax.Enabled = true;
            ImgBtnLuxuryTax.Enabled = true;
        }

        if (!IsPostBack)
        {
            bindBType();
            bindStatus();
            bindstate();
            bindcountry();
            //TabContainer1.ActiveTabIndex = 0;
        }
        //if (Session["NewCompId"] == null)
        //{
        //    TabContainer2.Enabled = false;
        //    TabContainer3.Enabled = false;
        //}

    }

    private void bindBType()
    {
        DataTable DT = dut.getdata("select BTypeId,BTypeName from ACC_BType");
        DDLTypeOfBuisness.DataSource = DT;
        DDLTypeOfBuisness.DataTextField = "BTypeName";
        DDLTypeOfBuisness.DataValueField = "BTypeId";
        DDLTypeOfBuisness.DataBind();
        ListItem lst = new ListItem("Others", "0");
        DDLTypeOfBuisness.Items.Add(lst);
    }

    private void bindStatus()
    {
        DataTable DT = dut.getdata("select CompStatusId,CompStatusName from ACC_CompStatus");
        DDLStatus.DataSource = DT;
        DDLStatus.DataTextField = "CompStatusName";
        DDLStatus.DataValueField = "CompStatusId";
        DDLStatus.DataBind();
        ListItem ls = new ListItem("Others", "0");
        DDLStatus.Items.Add(ls);
    }

    private void bindcountry()
    {
        DataTable dt = dut.getdata("select CountryId,CountryName from MAS_Country");
        DDLCountry.DataSource = dt;
        DDLCountry.DataTextField = "CountryName";
        DDLCountry.DataValueField = "CountryId";
        DDLCountry.DataBind();

    }
    private void bindstate()
    {
        DataTable DT = dut.getdata("select StateId,StateName from MAS_State");
        DDLState.DataSource = DT;
        DDLState.DataTextField = "StateName";
        DDLState.DataValueField = "StateId";
        DDLState.DataBind();

    }
    protected void DDLTypeOfBuisness_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDLTypeOfBuisness.SelectedValue == "0")
        {
            TxtOthers.Visible = true;
        }
        else
        {
            TxtOthers.Visible = false;
        }
    }
    protected void DDLStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDLStatus.SelectedValue == "0")
        {
            TxtStatus.Visible = true;
        }
        else
        {
            TxtStatus.Visible = false;
        }
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {

        int account = 0, invoice = 0, inventory = 0;
        if (chk.Items[0].Selected)
        {
            account = 1;
        }

        if (chk.Items[1].Selected)
        {
            invoice = 1;
        }

        if (chk.Items[2].Selected)
        {
            inventory = 1;
        }

        string[] arrstartdate = TxtAccountingYearStarting.Text.Split(new char[] { '/' });
        string startdate = arrstartdate[1].ToString() + "/" + arrstartdate[0].ToString() + "/" + arrstartdate[2].ToString();

        string[] arrenddate = TxtAccountingLast.Text.Split(new char[] { '/' });
        string enddate = arrenddate[1].ToString() + "/" + arrenddate[0].ToString() + "/" + arrenddate[2].ToString();

        SqlParameter[] parm ={
                                  new SqlParameter("@CompName", TextCompanyName.Text.Trim()),
                                  new SqlParameter("@TradingName", TxtTradingName.Text.Trim()),
                                  new SqlParameter("@BType", DDLTypeOfBuisness.SelectedValue),
                                  new SqlParameter("@OthBtype",TxtOthers.Text.Trim()),
                                  new SqlParameter("@StatusID",DDLStatus.SelectedValue),
                                  new SqlParameter("@OthStatus",TxtStatus.Text.Trim()),
                                  new SqlParameter("@IsAccEnabled",account),
                                  new SqlParameter("@IsInvcEnabled",invoice),
                                  new SqlParameter("@IsInvrEnabled",inventory),
                                  new SqlParameter("@BooksFromDate",startdate),
                                  new SqlParameter("@BooksEndDate",enddate),
                                  new SqlParameter("@CName","")

                               };

        //Response.Write(startdate);
        //Response.Write(enddate);
        try
        {

            dut.executeprocedure("usp_insert_ACC_Company", parm);
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "true");

            string strFinYear = _GetFinancialYear(startdate);

            string strCurrentDate = string.Empty;
            string[] str = startdate.Split('/');
            strCurrentDate = str[1] + "/" + str[0] + "/" + str[2];
            if (strFinYear != string.Empty)
            {
                string strStartDate = strCurrentDate;
                string strEndDate = "03/31/" + (Convert.ToInt32(strFinYear) + 1).ToString();
                SqlParameter[] sparamaccformat ={
                                               new SqlParameter("@EFFECTIVEDATE",startdate)
                                           };
                dut.executeprocedure("USP_ACC_InsertDefaultAccountingTransactionFormats", sparamaccformat);
                dut.executeprocedure("USP_IVR_InsertDefaultUoms");
                SqlParameter[] sparam ={
                                               new SqlParameter("@FINYEAR",strFinYear)
                                           };
                dut.executeprocedure("USP_ACC_CreateNewFinancialYear", sparam);
                SqlParameter[] sparam1 ={
                                                new SqlParameter("@MONTHID","0"),
                                                new SqlParameter("@STARTDATE",strStartDate),
                                                new SqlParameter("@ENDDATE",strEndDate)

                                            };
                dut.executeprocedure("USP_ACC_CreateDefaultMonthAndYearValues", sparam1);
                SqlParameter[] sparamuser =
                                    {
                                        new SqlParameter("@UserId","-1"),
                                        new SqlParameter("@UserName","admin"),
                                        new SqlParameter("@Password","demo"),
                                        new SqlParameter("@FirstName","admin"),
                                        new SqlParameter("@LastName",""),
                                        new SqlParameter("@GroupId",""),
                                        new SqlParameter("@IsGroupActive","0"),
                                        new SqlParameter("@Cname","")
                                    };
                int result = dut.executeprocedure("USP_SaveUserLogin", sparamuser);
                SqlParameter[] sparam2 ={
                                     new SqlParameter("@USERID","1"),
                                     new SqlParameter("@WORKINGDATE",strCurrentDate)
                                    };
                dut.executeprocedure("USP_ACC_SaveUserWorkingDate", sparam2);
            }

            lblMsg.Text = "Company created successfully...";

            DataTable DTCompany = dut.getdata("select ident_current('ACC_Company')");
            Session["NewCompId"] = DTCompany.Rows[0][0].ToString();
            //BtnSave.Enabled = false;
            //TabContainer1.ActiveTabIndex = 1;
        }
        catch (Exception ex)
        {
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = ex.Message;
        }

    }
    private string _GetFinancialYear(string strDate)
    {
        lblMsg.Text += strDate;
        string strFinYear = string.Empty;
        if (strDate != string.Empty)
        {
            string[] str = strDate.Split('/');
            string strMonth = str[1];
            string strYear = str[2];
            
            if (Convert.ToInt32(strMonth) >= 04 && Convert.ToInt32(strMonth) <= 12)
            {
                strFinYear = strYear;
            }
            else if (Convert.ToInt32(strMonth) >= 1 && Convert.ToInt32(strMonth) < 4)
            {
                strFinYear = (Convert.ToInt32(strYear) - 1).ToString();
            }
        }
        return strFinYear;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["NewCompId"] != null)
        {


            SqlParameter[] parm = {
                                  new SqlParameter("@CompId",Session["NewCompId"].ToString()),
                                   new SqlParameter("@Add1", TxtAdress1.Text.Trim()),
                                   new SqlParameter("@Add2", TxtAddress2.Text.Trim()),
                                   new SqlParameter("@Add3", TxtAddress3.Text.Trim()),
                                   new SqlParameter("@Add4", TxtAddress4.Text.Trim()),
                                   new SqlParameter("@Pincode", TxtPincode.Text.Trim()),
                                   new SqlParameter("@Country", DDLCountry.SelectedValue),
                                   new SqlParameter("@place", TxtPlace.Text.Trim()),
                                   new SqlParameter("@StateID", DDLState.SelectedValue),
                                   new SqlParameter("@Phone", TextBoxPhone.Text.Trim()),
                                   new SqlParameter("@Fax", TextBoxFax.Text.Trim()),
                                   new SqlParameter("@Email",TextBoxEmail.Text.Trim()),
                                   new SqlParameter("@WebSite", TextBoxWebsite.Text.Trim()),
                                   new SqlParameter("@Activity", TextBoxBuisnessActivity.Text.Trim()),
                                   new SqlParameter("@Misc1", TxtMiscellaneous1.Text.Trim()),
                                   new SqlParameter("@Misc2", TxtMiscellaneous2.Text.Trim())
                                   
                               };
            try
            {
                dut.executeprocedure("usp_insert_ACC_Company1", parm);
                //TabContainer1.ActiveTabIndex = 2;
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Company Contact Details Created successfully...";
            }
            catch (Exception ex)
            {
                 lblMsg.Attributes.Remove("class");
                 lblMsg.Attributes.Add("class", "false");
                 lblMsg.Text = ex.Message;
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (Session["NewCompId"] != null)
        {

            string Pan = "", taxdeduct = "", vat = "", StateTax = "", TaxService = "", centralexcise = "", luxury = "";
            if (TxtDatePermanant.Text.Trim() != "")
            {

                string[] PanDate = TxtDatePermanant.Text.Split(new char[] { '/' });
                Pan = PanDate[1].ToString() + "/" + PanDate[0].ToString() + "/" + PanDate[2].ToString();
            }

            if (TxtdateTaxDeduction.Text.Trim() != "")
            {

                string[] dateDedcutionTax = TxtdateTaxDeduction.Text.Split(new char[] { '/' });
                taxdeduct = dateDedcutionTax[1].ToString() + "/" + dateDedcutionTax[0].ToString() + "/" + dateDedcutionTax[2].ToString();
            }

            if (TxtVatDate.Text.Trim() != "")
            {
                string[] dateVat = TxtVatDate.Text.Split(new char[] { '/' });
                vat = dateVat[1].ToString() + "/" + dateVat[0].ToString() + "/" + dateVat[2].ToString();
            }

            if (TextBoxInterStateDate.Text.Trim() != "")
            {
                string[] dateInterState = TextBoxInterStateDate.Text.Split(new char[] { '/' });
                StateTax = dateInterState[1].ToString() + "/" + dateInterState[0].ToString() + "/" + dateInterState[2].ToString();
            }

            if (TxtDateServiceTax.Text.Trim() != "")
            {
                string[] dateServiceTax = TxtDateServiceTax.Text.Split(new char[] { '/' });
                TaxService = dateServiceTax[1].ToString() + "/" + dateServiceTax[0].ToString() + "/" + dateServiceTax[2].ToString();
            }


            if (TxtDateCentralExcise.Text.Trim() != "")
            {
                string[] CentralExciseDate = TxtDateCentralExcise.Text.Split(new char[] { '/' });
                centralexcise = CentralExciseDate[1].ToString() + "/" + CentralExciseDate[0].ToString() + "/" + CentralExciseDate[2].ToString();
            }

            if (DateLuxuryTax.Text.Trim() != "")
            {
                string[] datetaxluxury = DateLuxuryTax.Text.Split(new char[] { '/' });
                luxury = datetaxluxury[1].ToString() + "/" + datetaxluxury[0].ToString() + "/" + datetaxluxury[2].ToString();
            }



            int isvat = 0, iscst = 0, issst = 0, isce = 0, islt = 0;
            if (CHKVat.Checked == true)
            {
                isvat = 1;
            }
            if (CHKInterstate.Checked == true)
            {
                iscst = 1;
            }
            if (CHKServiceTax.Checked == true)
            {
                issst = 1;
            }
            if (CHKCentralExcise.Checked == true)
            {
                isce = 1;
            }
            if (CHKLuxuryTax.Checked == true)
            {
                islt = 1;
            }

            
            SqlParameter[] parm = {
                                   new SqlParameter("@CompId",Session["NewCompId"].ToString()),
                                    new SqlParameter("@Pan", TxtPermanant.Text.Trim()),
                                    new SqlParameter("@PanDate", Pan),
                                    new SqlParameter("@TDSNO", TxtTaxdeduction.Text.Trim()),
                                    new SqlParameter("@TDSDate", taxdeduct),
                                    new SqlParameter("@IsVatEnable",isvat),
                                    new SqlParameter("@VatNo",TxtBoxVat.Text.Trim()),
                                    new SqlParameter("@VatDate", vat  ),
                                    new SqlParameter("@IsCSTEnable",iscst),
                                    new SqlParameter("@CSTNo",TextBoxinnerState.Text.ToString()),
                                    new SqlParameter("@CSTDate",StateTax),
                                    new SqlParameter("@ISSTEnabled",issst),
                                    new SqlParameter("@SERTNo",TextBoxServiceTax.Text.Trim()),
                                    new SqlParameter("@SERTNDate",TaxService),
                                    new SqlParameter("@ISCEEnabled",isce),
                                    new SqlParameter("@CEPAN",TextBoxCentralExcise.Text.Trim()),
                                    new SqlParameter("@CEPANDate",centralexcise),
                                    new SqlParameter("@ISLTENABLED",islt),
                                    new SqlParameter("@LTNO", TextBoxLuxuryTax.Text.ToString()),
                                    new SqlParameter("@LTNDATE",luxury),
                                     
                                };
            try
            {
               dut.executeprocedure("usp_insert_ACC_Company2", parm);
                //TabContainer1.ActiveTabIndex = 0;
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Company Registration details Created successfully...";
            }
            catch (Exception ex)
            {
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = ex.Message;
            }
        }
        
    }

    protected void CHKVat_CheckChanged(object o, EventArgs e)
    {
        if (CHKVat.Checked)
        {
            TxtBoxVat.Enabled = true;
            TxtVatDate.Enabled = true;
            
        }
        else
        {
            TxtBoxVat.Enabled = false;
            TxtVatDate.Enabled = false;
            ImgBtnVatDate.Enabled = false;
        }
    }

    protected void CHKInterstate_CheckChanged(object o, EventArgs e)
    {
        if (CHKInterstate.Checked)
        {
            TextBoxinnerState.Enabled = true;
            TextBoxInterStateDate.Enabled = true;
        }
        else
        {
            TextBoxinnerState.Enabled = false;
            TextBoxInterStateDate.Enabled = false;
            ImgBtnInterState.Enabled = false;
        }

    }

    protected void CHKServiceTax_CheckChanged(object o, EventArgs e)
    {
        if (CHKServiceTax.Checked)
        {
            TextBoxServiceTax.Enabled = true;
            TxtDateServiceTax.Enabled = true;
        }
        else
        {
            TextBoxServiceTax.Enabled = false;
            TxtDateServiceTax.Enabled = false;
            ImgBtnServiceTax.Enabled = false;
        }
    }

    protected void CHKCentralExcise_CheckChanged(object o, EventArgs e)
    {
        if (CHKCentralExcise.Checked)
        {
            TextBoxCentralExcise.Enabled = true;
            TxtDateCentralExcise.Enabled = true;
        }
        else
        {
            TextBoxCentralExcise.Enabled = false;
            TxtDateCentralExcise.Enabled = false;
            ImgBtnCentralExcise.Enabled = false;
        }
    }

    protected void CHKLuxuryTax_CheckChanged(object o, EventArgs e)
    {
        if (CHKLuxuryTax.Checked)
        {
            TextBoxLuxuryTax.Enabled = true;
            DateLuxuryTax.Enabled = true;
        }
        else
        {
            TextBoxLuxuryTax.Enabled = false;
            DateLuxuryTax.Enabled = false;
            ImgBtnLuxuryTax.Enabled = false;
        }
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
       // Response.Redirect("CompanyView.aspx");

    }
    protected void BtnCancel_Click(object sender, EventArgs e)
    {
        
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        

    }
}


